//
//  LGCiTangPhotoViewController.m
//  ligenyun
//
//  Created by Mac on 2018/3/26.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "LGCiTangJiSiViewController.h"
#import "LGCiTangJiSiCollectionViewCell.h"
#import "STPhotoUrlImageBrowerController.h"
@interface LGCiTangJiSiViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,STPhotoUrlImageBrowerControllerDelegate>
@property(nonatomic, strong) NSArray                     *dataSouce;
@property(nonatomic, strong) UICollectionView                     *collectionView;
@property(nonatomic, weak) STButton                     *iconButton;
@end

@implementation LGCiTangJiSiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"祭祀";
    self.dataSouce = @[@"1",@"2",@"8",@"9",@"10",
                       @"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19"];
    [self configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
    flow.minimumLineSpacing = 5;
    flow.minimumInteritemSpacing = 5;
    flow.sectionInset = UIEdgeInsetsMake(10, 20, 0, 20);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:UIScreenFrame collectionViewLayout:flow];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[LGCiTangJiSiCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
}
#pragma --mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSouce.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(photoItemWith, [LGCiTangJiSiCollectionViewCell cellHeight]);
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LGCiTangJiSiCollectionViewCell * item =[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    UIImage * image = [UIImage imageNamed:self.dataSouce[indexPath.row]];
    [item.iconButton setBackgroundImage:image forState:UIControlStateNormal];
    __weak typeof(self) weakSelf =  self;
    [item.iconButton setClicAction:^(UIButton *sender) {
       // [weakSelf onSelctedIconButton:sender indexPath:indexPath];
    }];
    return item;
}
#pragma --mark UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedIconButton:(id)sender indexPath:(NSIndexPath*)indexPath{
    self.iconButton = sender;
    NSMutableArray * models = [NSMutableArray new];
    for (NSString * name in self.dataSouce) {
        STUrlPhotoModel * model = [STUrlPhotoModel  new];
        model.originImage = [UIImage imageNamed:name];
        [models addObject:model];
    }
    STPhotoUrlImageBrowerController * vc = [[STPhotoUrlImageBrowerController alloc] initWithArray:models curentIndex:indexPath.row];
    vc.shouldHideBottomView = YES;
    vc.STPhotoUrlImageBrowerControllerdelegate = self;
    [self presentViewController:vc animated:NO completion:nil];
}
#pragma mark --STPhotoUrlImageBrowerControllerDelegate
- (UIView*)STPhotoSystemBrowserControllerDidScrollToIndexpath:(NSIndexPath*)indexPath model:(STUrlPhotoModel*)model{
    return   self.iconButton;
}
@end

